﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Service.Forms
{
    public partial class UserListForm : SimplelListForm
    {
        public UserListForm()
        {
            InitializeComponent();
        }

        public override void InitializeBinding()
        {
            gridControl1.DataSource = ViewModel["data"];
            repFRIGHTS.DataSource = Fis.Win.Utils.AppSession.RightsCache();
            repFSTATUS.DataSource = Fis.Win.Utils.AppSession.StateCache();
        }

        protected override void AddNewProcess()
        {
            var v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.UserEditForm>("UserViewProvider");
            if (((DialogResult)v.ViewResult) == System.Windows.Forms.DialogResult.OK)
            {
                RefreshProcess();
            }
        }

        protected override void ModifyProcess()
        {
            var data = gridView1.GetFocusedDataRow();

            if (data == null)
                return;

            var param = new System.Collections.Hashtable();
            param["SID"] = data["SID"];
            var v = Fis.Win.UIManager.ShowEdit<Fis.Service.Forms.UserEditForm>("UserViewProvider", Agile.Engine.View.ViewState.Modify, param);
            data.ItemArray = (v.ViewModel["data"] as DataTable).Rows[0].ItemArray;
        }

        protected override void DeleteProcess()
        {
            var data = gridView1.GetFocusedDataRow();

            if (data == null)
                return;

            var param = new System.Collections.Hashtable();
            param["SID"] = data["SID"];

            if (ViewProvider.ExecuteUpdate(param))
            {
                data.Delete();
                gridControl1.RefreshDataSource();
            }
        }

        protected override void RefreshProcess()
        {
            ViewModel = ViewProvider.ExecuteQuery(ViewModel);
            InitializeBinding();
        }

        protected override void FindProcess(object filter)
        {
            gridView1.ApplyFindFilter(filter.ToString());
        }

        private void UserListForm_Load(object sender, EventArgs e)
        {
        }
    }
}
